package net.kuratkoo.jidelnicekvut;

import android.content.Context;
import android.database.Cursor;
import java.sql.Time;
import java.util.ArrayList;

public class CanteenHourSet extends ArrayList<CanteenHour> {
    private static final String TAG = "JidelnicekVUT|CanteenHourSet";
    private Context context;

    public CanteenHourSet(Context context) {
        this.context = context;
    }

    public CanteenHourSet getByCanteenId(Integer canteenId) {
        this.clear();
        DatabaseAdapter db = new DatabaseAdapter(this.context);
        Cursor c = db.getDb().query("canteen_hours", new String[]{"canteen_id", "time_from", "time_to", "day"}, "canteen_id = ?", new String[]{canteenId.toString()}, null, null, "canteen_id, day, time_from");

        while (c.moveToNext()) {
            CanteenHour ch = new CanteenHour(this.context);
            ch.canteenId = canteenId;
            ch.day = c.getInt(c.getColumnIndex("day"));
            String[] timeFrom = c.getString(c.getColumnIndex("time_from")).split(":");
            ch.from = new Time(Integer.parseInt(timeFrom[0]), Integer.parseInt(timeFrom[1]), Integer.parseInt(timeFrom[2]));
            String[] timeTo = c.getString(c.getColumnIndex("time_to")).split(":");
            ch.to = new Time(Integer.parseInt(timeTo[0]), Integer.parseInt(timeTo[1]), Integer.parseInt(timeTo[2]));
            this.add(ch);
        }
        
        c.close();
        db.close();
        return this;
    }
}
